
var fm = Ext.form;
var base_url = '<?php echo base64_decode($_GET['baseUrl']);?>';
var total_row = 20;
var proxy = new Ext.data.ScriptTagProxy({
 	api: {
        read : base_url+'admin/products/grid.html',
        create : base_url+'admin/products/add.html',
        update: base_url+'admin/products/add.html',
        destroy: base_url+'admin/products/delete.html'
    }
});

var writer = new Ext.data.JsonWriter({
    encode: true
});

var store = new Ext.data.JsonStore({
	start : 'start',
	remoteSort: true,
    totalProperty: 'total',
    proxy: proxy,
    root: 'data',
    fields : [
              {name: 'id'},
{name: 'name'},
{name: 'product_code'},
{name: 'thumb',type: 'string'},
{name: 'edit',type: 'string'},
{name: 'onhome',type: 'bool'},
{name: 'showtype',type: 'bool'},
{name: 'is_no_sale',type: 'bool'},
{name: 'hot_item',type: 'bool'}
          ],
	  listeners: {
	      save: function(s, b, d) {
	    	  store.save();
	      }
	  },
    writer: writer
});


var filters = new Ext.ux.grid.GridFilters({
    // encode and local configuration options defined previously for easier reuse
    encode: true, // json encode the filter query
    local: true,   // defaults to false (remote filtering)
    filters: [
        {type: 'string',dataIndex: 'name'},
        {type: 'string',dataIndex: 'product_code'},

    ]
});
var createColModel = function () {

    var columns = [
    {header: 'ID',sort: 'id',dataIndex: 'id'},
	{header: 'Name',sortable: true,sort: 'name',dataIndex: 'name'},
	{header: 'Product Code',sortable: true,sort: 'product_code',dataIndex: 'product_code'},
	{header: 'Image',dataIndex: 'thumb',sortable: false},
	{header: 'On Home',dataIndex: 'onhome',sortable: false,xtype:'checkcolumn'},               
        {header: 'La TBSan Xuat',dataIndex: 'is_no_sale',sortable: false,xtype:'checkcolumn'},       
        {header: 'Show Type',dataIndex: 'showtype',sortable: false,xtype:'checkcolumn'},   
        {header: 'Hot Item',dataIndex: 'hot_item',sortable: false,xtype:'checkcolumn'},
        {header: 'Edit',dataIndex: 'edit',sortable: false}        
               ];

    return new Ext.grid.ColumnModel({
        columns: columns,
        defaults: {
            sortable: true
        }
    });
};
pagingBar = new Ext.PagingToolbar({
            store: store,
            pageSize: total_row,
            displayInfo: true,
            displayMsg: 'Displaying item {0} - {1} of {2}',
            emptyMsg: "No item to display",
            plugins: new Ext.ux.ProgressBarPager()
        });
Ext.onReady(function() {
    Ext.QuickTips.init();

    var newsGrid = new Ext.grid.GridPanel({
        renderTo: 'news-grid',
        iconCls: 'icon-grid',
        frame: true,        
        height: eval(Ext.getBody().getViewSize().height - 280),
        store: store,
        plugins: [filters],
        colModel: createColModel(),
        listeners: {
            render: {
                fn: function(){
                    store.load({
                        params: {
                            start: 0,
                            limit: total_row
                        }
                    });
                }
            }
        },
        tbar: [new Ext.form.ComboBox({
                     triggerAction: 'all',
                     lazyRender:false,
                     width:40,
                     mode: 'local',
                     store: new Ext.data.ArrayStore({
                         fields: [
                                  'id',
                                  'value'
                                 ],
                        data: [[10,'10'],[20,'20'],[50,'50'],[100,'100']]
                     }),
                     valueField: 'id',
                     displayField: 'value',
                     listeners:{
                          'select': function(combo,record ,index){
                        	  total_row = combo.getValue();
                        	  store.reload({params:{start:0, limit:total_row}});
                                  pagingBar.pageSize = total_row;
                        	  pagingBar.render();
                          }
                     }
                })
        ,'-',{
            text: 'Add',
            iconCls: 'silk-add',
            handler: onAdd
        }, '-', {
            text: 'Delete',
            iconCls: 'silk-delete',
            handler: onDelete
        }, '-'],
        bbar: pagingBar,
        viewConfig: {
            forceFit: true
        }
    });

    function onAdd(btn, ev) {
       window.location.href = base_url+'admin/products/edit';
    }

    function onDelete() {
        Ext.MessageBox.show({
            title:'Delete item',
            msg: 'Do you really want to delete this item ?',
            buttons: Ext.MessageBox.YESNO,
            animEl: 'samplebutton',
            icon: Ext.MessageBox.QUESTION,
            fn:function(buttonId){
                if (buttonId == 'yes'){
                    var recs = newsGrid.getSelectionModel().getSelections() ;
                    if (!recs) {
                        return false;
                    }
                    for(var i in recs)
                    {
                        newsGrid.store.remove(recs[i]);
                    }
                }
            }
        });
    }    
});
function formatDate(value){
    return value ? value.dateFormat('d-m-Y') : '';
}
function formatLanguage(value){
	if(value!='')
		return eval('langList.'+value);
	else return '';
}



